//
//  YXQrCodeView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/22.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXQrCodeView.h"

@interface YXQrCodeView ()
@property (nonatomic ,strong) UIImageView *headerImage;
@property (nonatomic ,strong) UIImageView *codeImage;
@property (nonatomic ,strong) UILabel *invitationLab;
@property (nonatomic ,strong) UIView *myCodeView;
@property (nonatomic ,strong) UILabel *myCodeLab;
@property (nonatomic ,strong) UILabel *codeLab;
@property (nonatomic ,strong) UIButton *shareBtn;
@end

@implementation YXQrCodeView

- (UIImageView *)headerImage {
    if (!_headerImage) {
        _headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_001"]];
    }
    return _headerImage;
}

- (UIImageView *)codeImage {
    if (!_codeImage) {
        _codeImage = [UIImageView new];
    }
    return _codeImage;
}

- (UILabel *)invitationLab {
    if (!_invitationLab) {
        _invitationLab = [UILabel setLabelWithText:@"" fontSize:16 color:color_textOne];
        _invitationLab.textAlignment = NSTextAlignmentCenter;
        _invitationLab.attributedText = [self makeInvitationLabAttributed];
    }
    return _invitationLab;
}

- (UIView *)myCodeView {
    if (!_myCodeView) {
        _myCodeView = [[UIView alloc] init];
        _myCodeView.layer.masksToBounds = YES;
        _myCodeView.layer.cornerRadius = 8;
        _myCodeView.layer.borderWidth = 1.0f;
        _myCodeView.layer.borderColor = color_lineColor.CGColor;
    }
    return _myCodeView;
}

- (UILabel *)myCodeLab {
    if (!_myCodeLab) {
        _myCodeLab = [UILabel setLabelWithText:@"我的推荐码" fontSize:20 color:[UIColor blackColor]];
    }
    return _myCodeLab;
}

- (UILabel *)codeLab {
    if (!_codeLab) {
        YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
        _codeLab = [UILabel setLabelWithText:[NSString stringWithFormat:@"%@",model.pid] fontSize:20 color:color_textTwo];
    }
    return _codeLab;
}
- (UIButton *)shareBtn {
    if (!_shareBtn) {
        _shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _shareBtn.backgroundColor = color_globalColor;
        [_shareBtn setTitle:@"分享给朋友" forState:(UIControlStateNormal)];
        [_shareBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _shareBtn.layer.masksToBounds = YES;
        _shareBtn.layer.cornerRadius = 8.0f;
        [_shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _shareBtn;
}

- (void)shareBtnAction:(UIButton *)sender {
    self.clickShareBtnBlock();
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self logoQrCode];

    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.headerImage];
    [self addSubview:self.codeImage];
    [self addSubview:self.invitationLab];
    [self addSubview:self.myCodeView];
    [self.myCodeView addSubview:self.myCodeLab];
    [self.myCodeView addSubview:self.codeLab];
    [self addSubview:self.shareBtn];
    
}

-(void)logoQrCode{
    
    //
    //    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    //    NSLog(@"%@",filters);
    
    //二维码过滤器
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //设置过滤器默认属性 (老油条)
    [qrImageFilter setDefaults];
    
    //将字符串转换成 NSdata (虽然二维码本质上是 字符串,但是这里需要转换,不转换就崩溃)
    NSData *qrImageData = [@"xxx" dataUsingEncoding:NSUTF8StringEncoding];
    
    //我们可以打印,看过滤器的 输入属性.这样我们才知道给谁赋值
    //    NSLog(@"%@",qrImageFilter.inputKeys);
    /*
     inputMessage,        //二维码输入信息
     inputCorrectionLevel //二维码错误的等级,就是容错率
     */
    
    
    //设置过滤器的 输入值  ,KVC赋值
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    
    //取出图片
    CIImage *qrImage = [qrImageFilter outputImage];
    
    //但是图片 发现有的小 (27,27),我们需要放大..我们进去CIImage 内部看属性
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    
    //转成 UI的 类型
    UIImage *qrUIImage = [UIImage imageWithCIImage:qrImage];
    
    //----------------给 二维码 中间增加一个 自定义图片----------------
    //开启绘图,获取图形上下文  (上下文的大小,就是二维码的大小)
    UIGraphicsBeginImageContext(qrUIImage.size);
    
    //把二维码图片画上去. (这里是以,图形上下文,左上角为 (0,0)点)
    [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)];
    
    
    //再把小图片画上去
    UIImage *sImage = [UIImage imageNamed:@"logo"];
    
    
//        UIImageView *sImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 60, 60)];
//        [self addSubview:sImageView];
//        sImageView.image = sImage;
//        // 类似于clip，使用masksToBounds阴影效果无效
//        sImageView.layer.masksToBounds = YES;
//        //图层的圆角半径
//        sImageView.layer.cornerRadius = 50;
    
    
    
    CGFloat sImageW = 100;
    CGFloat sImageH= sImageW;
    CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5;
    CGFloat sImgaeY = (qrUIImage.size.height - sImageH) * 0.5;
    
    [sImage drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)];
    
    //获取当前画得的这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    
    //设置图片
    self.codeImage.image = finalyImage;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).offset(40);
        make.centerX.equalTo(weakSelf.mas_centerX);
        [_headerImage sizeToFit];
    }];
    
    [_codeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.headerImage.mas_bottom).offset(20);
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    
    [_invitationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.equalTo(weakSelf.codeImage.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 20));
    }];
    
    [_myCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.invitationLab.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.mas_left).offset(20);
        make.right.equalTo(weakSelf.mas_right).offset(-20);
        make.height.offset(50);
    }];
    
    [_myCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.myCodeView.mas_centerY);
        make.left.equalTo(weakSelf.myCodeView.mas_left).offset(20);
        [_myCodeLab sizeToFit];
    }];
    
    [_codeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.myCodeView.mas_centerY);
        make.left.equalTo(weakSelf.myCodeLab.mas_right).offset(30);
        [_codeLab sizeToFit];
    }];
    
    [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.myCodeView.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.mas_left).offset(20);
        make.right.equalTo(weakSelf.mas_right).offset(-20);
        make.height.offset(50);
    }];
}

#pragma mark - makeAttribute
- (NSMutableAttributedString *)makeInvitationLabAttributed {
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:[YXUserInfoManager getUserInfo].name attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:color_blueColor}];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",@"邀请您注册卡友"
                                                                                            ] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:color_textOne}];
    [string1 insertAttributedString:string2 atIndex:string1.length];
    return string1;
}
@end
